home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1998 January / PC Answers Issue 49 Cover CD January 1998.iso / Apps / Director / DATA.Z / Behavior Library.cst / 00051_UI Drag Snap to Sprite List.ls < prev    next >
Encoding:
Text File  |  1997-05-09  |  4.7 KB  |  157 lines

  1. -- Gesture Snap to Sprite List
  2.  
  3.  
  4. property  spritestring     --  the string that is set by the user, a list without brackets
  5. property  snapList         --  the list that is built from spritestring, the snap targets
  6. property  snapDistance     --  how many pixels from the reg point of targets to snap
  7. property  xoffset, yoffset --  maintains offfset of cursor after mouse down
  8. property  snapboxes        --  a list [sprite, [top: , bottom: , left: , right]],...
  9. property  tracking         --  if the mouse is now dragging, true
  10. property  currentboxsprite  -- 0 if not in any box, otherwise snapped to sprite
  11. property  boxindex         -- position in snapboxes of currentbox
  12.  
  13. on beginsprite  me
  14.   
  15.   set snapboxes = []
  16.   set the snaplist of me = convertstring (spriteString)
  17.   
  18.   repeat with isprite in snaplist    
  19.     set boxrecord = [┬¼
  20.     #top:the locv of sprite isprite  - snapDistance,┬¼
  21.     #bottom : the locv of sprite isprite  + snapDistance,┬¼
  22.     #left   : the loch of sprite isprite  - snapDistance,┬¼
  23.     #right  : the loch of sprite isprite  + snapDistance ] 
  24.     
  25.     add snapboxes, [isprite,boxrecord]
  26.   end repeat
  27.   
  28. end
  29.  
  30. on prepareframe me
  31.   if tracking then
  32.     if currentboxsprite = 0 then  
  33.       -- tracking and not yet snapped
  34.       if inanybox (me, the mouseH - xoffset, the mouseV - yoffset) then 
  35.         -- tracking, in a box, snap it
  36.         snap me, currentboxsprite
  37.         exit
  38.         
  39.         
  40.       else 
  41.         -- tracking, not in a box, drag it
  42.         set the locH of sprite the spritenum of me = the MouseH - xoffset
  43.         set the locV of sprite the spritenum of me = the MouseV - yoffset   
  44.         exit
  45.       end if
  46.     end if
  47.     
  48.     -- snapped, tracking, check that mouse is still in the box
  49.     if inabox (me, the mouseH - xoffset, the mouseV - yoffset, boxindex) then
  50.       -- snapped, tracking, and still in the box
  51.       -- don't move the thing, it's still snapped
  52.       exit
  53.     else
  54.       -- was snapped, now outside of box, drag it
  55.       set the locH of sprite the spritenum of me = the MouseH - xoffset
  56.       set the locV of sprite the spritenum of me = the MouseV - yoffset   
  57.       exit
  58.     end if  
  59.   end if
  60.   
  61. end
  62.  
  63.  
  64. on mousedown me
  65.   set tracking  = TRUE
  66.   set xoffset = the mouseH - the locH of sprite the spritenum of me
  67.   set yoffset = the mouseV - the locV of sprite the spritenum of me
  68.   
  69. end
  70.  
  71. on mouseup me
  72.   set tracking = FALSE
  73. end
  74.  
  75. on snap me, snaptarget
  76.   set the locH of sprite the spritenum of me = the locH of sprite snapTarget 
  77.   set the locV of sprite the spritenum of me = the locV of sprite snapTarget  
  78. end
  79.  
  80.  
  81. on inanybox me,x,y
  82.   -- sets currentboxsprite to spritenum of the box the cursor is in, 0 if no box
  83.   -- returns TRUE if inanybox, false otherwise
  84.   set index = 0
  85.   repeat with ibox in snapboxes
  86.     set index = index + 1
  87.     set boxvals = getAt(ibox, 2)  
  88.     if (x < (getprop (boxvals, #right))) and ┬¼
  89.        (x > (getprop (boxvals, #left))) and ┬¼
  90.        (y < (getprop (boxvals, #bottom))) and ┬¼
  91.        (y > (getprop (boxvals, #top))) then
  92.       set currentboxsprite = getAt (ibox, 1)
  93.       set boxindex = index
  94.       return 1
  95.     end if    
  96.   end repeat
  97.   
  98.   set boxindex = 0
  99.   set currentboxsprite = 0
  100.   return 0
  101. end
  102.  
  103. on inabox me,x,y,boxindex
  104.   -- returns TRUE if in boxindex box, false otherwise  
  105.   -- used for tacking if the mouse leaves the current snap box
  106.   set ibox = getAt(snapboxes, boxindex)  
  107.   set boxvals = getAt(ibox, 2)  -- boxvals is a propertylist [top:,bottom:,etc.]  
  108.   if   x < getprop (boxvals, #right) and ┬¼
  109.        x > getprop (boxvals, #left) and ┬¼
  110.        y < getprop (boxvals, #bottom) and ┬¼
  111.        y > getprop (boxvals, #top) then
  112.     return 1
  113.   end if
  114.   
  115.   set boxindex = 0
  116.   set currentboxsprite = 0
  117.   return 0
  118. end
  119.  
  120.  
  121.  
  122. ---
  123.  
  124. on getPropertyDescriptionList
  125.   
  126.   set p_list = [ ┬¼
  127.   #spritestring: [ #comment: "Sprite List:", ┬¼
  128.                     #format: #string, ┬¼
  129.                    #default: "4,5,7,8" ], ┬¼
  130.   #snapDistance: [ #comment: "Snap Distance:", ┬¼
  131.                     #format: #integer, ┬¼
  132.                    #default:  8 ] ┬¼
  133.                  ]
  134.   return p_list 
  135. end
  136.  
  137. on getBehaviorDescription
  138.   return ┬¼
  139. "Cause a sprite to snap to the position of any of a list of sprites while dragging." & RETURN & ┬¼
  140. "PARAMETERS:" & RETURN & ┬¼
  141. "ΓÇó Sprite List - Enter the channel numbers of sprites to snap to, separated by commas." & RETURN & ┬¼
  142. "ΓÇó Snap Distance - Enter the range in pixels within which snapping will occur.  If set to zero, the sprite being dragged always snaps to the location of the nearest list member."
  143.   
  144. end
  145.  
  146.  
  147.  
  148. on convertstring spritestring
  149.   set newlist = []
  150.   set count = 1
  151.   repeat while item count of spritestring <> ""
  152.     add newlist, value (item count of spritestring)
  153.     set count = count + 1
  154.   end repeat
  155.   return newlist
  156. end
  157.